/**
 * 
 */
package cw.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cw.entity.ArrangeGroup;
import cw.entity.Competition;
import cw.entity.Group;
import cw.entity.Team;
import cw.entity.User;
import cw.page.PageUtil;
import cw.service.ArrangeGroupService;
import cw.service.CompetitionService;
import cw.service.GroupService;
import cw.service.TeamService;
import cw.service.UserService;
import cw.serviceImp.UserServiceImpl;
import cw.util.DES;
import cw.util.Log;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping("competition")
public class CompetitionController {
	
	private static Logger logger = Logger.getLogger(CompetitionController.class);

	@Resource
	private CompetitionService competitionServiceImpl;
	
	@Resource
	private ArrangeGroupService arrangeGroupServiceImpl;
	
	
	
	/**
     * 默认页面
     */
    @RequestMapping(method = {RequestMethod.POST,RequestMethod.GET})
    public String list(HttpServletRequest request){
        return "showCompetition";
    }
	
	@RequestMapping(value="json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> showCompetition(HttpServletRequest request) {
		/*
		private int pageSize;//每页显示的条数 
	    private int recordCount;//总共的条数 
	    private int currentPage;//当前页面 
	    //总页数 int getPageCount()     
        //包含，起始索引为0 getFromIndex()
        //不包含 getToIndex()
	    */
		
	    
      String teamId = request.getParameter("teamId");
      
      String teamTid = request.getParameter("teamTid");
      
      String description =request.getParameter("description");
      String id = request.getParameter("id");
      
      Integer total = competitionServiceImpl.getTotal(teamId,teamTid, description, id);
      System.out.print("---"+total);
      
      Integer pageSize = Integer.parseInt(request.getParameter("rows"));
      
      
      Integer page = Integer.parseInt(request.getParameter("page"));
      
      PageUtil pageUtil = new PageUtil(pageSize,total,page);
      
      List<Competition> arrangeGroups = competitionServiceImpl.queryCompetition(teamId, teamTid, description, id, pageUtil.getFromIndex(), pageUtil.getToIndex());
      
      Map<String,Object> map = new HashMap<>();
      
      map.put("total", total);
      map.put("rows", arrangeGroups);
       
      return map;
   }
	
	/**
     * 创建
     */
    @RequestMapping(value = "create", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String createForm(HttpServletRequest request) {
    	
    	//得到组数
    	//int num = Integer.parseInt(request.getParameter("num"));
    	int algorithm =1;
    	int type =1;
    	if(type==1){
    		
    	}else{
    		
    	}
    	Random ran = new Random();
    	//第一步：取得所有组的id
    	List<Integer> list = arrangeGroupServiceImpl.querGroupId();
    	//第二步：取得每组对应的所有队id
    	for(Integer groupId:list){    		 
    		List<Integer> group1 = arrangeGroupServiceImpl.queryGroup(groupId);
    		int size = group1.size();
    		List<Integer> group = new ArrayList<>();
    		if(type==1){
    			 group= arrangeGroupServiceImpl.queryGroup(groupId);
        	}else{
        		for(int i=0;i<size;i++){
        			Integer id = group1.get(ran.nextInt(group1.size()));
        			group.add(id);
        			group1.remove(id);
        		}
        	}
    		
    		//第三步：根据不同的算法，不同的比赛类型进行不同的竞赛安排
    		System.out.println("===================="+groupId);
    		competitionServiceImpl.arrangeCompetition(group);
    	}
    		
        
        return "success";
    }
    
    

    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id")Integer id) {
    	int num = competitionServiceImpl.deleteByPrimaryKey(id);
    	Log.write("删除成功!"+new Date().toString());
        return "success";
    }
	
}
